﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Windows.Controls;
using XYAC.Framework.MonitorTireMaterialManual.IServices;
using XYAC.Framework.MonitorTireMaterialManual.ViewModels;

namespace XYAC.Framework.MonitorTireMaterialManual.Views
{
    /// <summary>
    /// QualityDataUploadView.xaml 的交互逻辑
    /// </summary>
    public partial class GbQualityDataUploadView : UserControl
    {
        public GbQualityDataUploadView(QualityDataUploadViewModel viewModel,
            ILogger<QualityDataUploadView> logger,
            IServiceProvider provider)
        {
            InitializeComponent();

            this.DataContext = this.ViewModel = viewModel;
            this.Logger = logger;
            this.Provider = provider;
            RefershDetectionMachine();
        }
        /// <summary>
        /// 刷新检测机台
        /// </summary>
        private void RefershDetectionMachine()
        {
            try
            {
                using (var scope = this.Provider.CreateScope())
                {
                    var service = scope.ServiceProvider.GetService<ILocalDataService>();
                    var DetectionMachineList = service.GetAll<Entities.EquipmentMapping>();

                    //this.TabMachines.ItemsSource = DetectionMachineList;
                    //this.cmbDeviceType.SelectedIndex = 0;
                    this.cmbDeviceType.SelectedItem = ViewModel.MachineTypeList[0];

                    //以设备类型为1的清单初始设备列表
                    //ViewModel.SelectPlatformByType("1");
                    //lvMachineList.SelectedIndex = 0;

                    //ViewModel.CurrentSelection = cmbDeviceType.Items[0] as Item;
                    //this.ViewModel.SelectedDetectionMachine = DetectionMachineList.FirstOrDefault();
                }
            }
            catch (Exception)
            {

            }
        }
        public ILogger Logger { get; }
        public IServiceProvider Provider { get; }
        public QualityDataUploadViewModel ViewModel { get; }

        private void DatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            this.ViewModel.UpdateCustomizePlanDateWithConfig((sender as DatePicker).SelectedDate);

        }

        private void lvMachineList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                if (e.AddedItems[0] is Entities.EquipmentMapping cbi)
                {
                    this.ViewModel.SelectedDetectionMachine = cbi;
                    this.ViewModel.RefershInspectData();
                }
            }
        }

        private void cmbDeviceType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                if (e.AddedItems[0] is Item cbi)
                {
                    ViewModel.SelectPlatformByType(cbi.Value);
                    lvMachineList.SelectedIndex = 0;
                }
            }

        }

        private void lvMachineList_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var ojb = sender as ListView;
            if (ojb.SelectedItem != null)
            {
                var cbi = ojb.SelectedItem as Entities.EquipmentMapping;
                this.ViewModel.SelectedDetectionMachine = cbi;
                this.ViewModel.RefershInspectData();
            }
        }

        private void btnPreCheck_Click(object sender, System.Windows.RoutedEventArgs e)
        {

        }
    }
}
